package com.easyapi.rabbitmq;

import com.easyapi.common.emun.StatusCode;
import com.easyapi.common.exception.BizException;
import com.easyapi.service.DatasourceService;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.Objects;

/**
 * @author: javadadi
 * @Time: 17:15
 * @ClassName: RabbitmqConsumer
 */
@Component
@Slf4j
public class RabbitmqConsumer {
    // 定义常量
    public static final String DATASOURCE_QUEUE = "DatasourceDirectQueue";
    public static final String API_QUEUE = "ApiDirectQueue";

    @Resource
    private DatasourceService datasourceService;

    @RabbitListener(queues = DATASOURCE_QUEUE)
    @RabbitHandler
    public void  consumerDatasource(String message){
        JsonObject jsonObject = JsonParser.parseString(message).getAsJsonObject();
        long id = jsonObject.get("id").getAsLong();
        String action = jsonObject.get("action").getAsString();
        if(StringUtils.isBlank(action)||id<=0){
            log.error("error message when{}",System.currentTimeMillis());
            throw new BizException(StatusCode.SYSTEM_ERROR);
        }
        datasourceService.execute(action,id);
    }

    @RabbitListener(queues = API_QUEUE)
    @RabbitHandler
    public void consumerApi(String message){
        System.out.println(message);
    }
}
